import axios from 'axios';
import {Message,Loading} from 'element-ui'
import router from '../src/router'
let loading;
function startLoading(){ 
    loading=Loading.service({
        lock:true,
        text:"加载中。。",
        background:'rgba(0,0,0,0,7)'
    })
}
function endLoading(){
    loading.close();
}
//请求拦截
axios.interceptors.request.use(config=>{
    startLoading();
    if(localStorage.token){
        //设置统一的请求header
        //config.headers.Authorization=localStorage.token;
        //config.headers['jwt'] = jwt;
        config.headers.UserToken=localStorage.token;
    }
    return config;
},err=>{
    return Promise.reject(err);
})
//响应拦截
axios.interceptors.response.use(response=>{
    endLoading();
    if(response.data.code!="200"){
        Message.error(response.data.message);
        //return Promise.reject("no");
    }
    return response;
},err=>{
    endLoading();
    Message.error("服务器异常,请稍等...");
    //const {status}=err.response;
    //代表token失效
    //if(status==401){
        //Message.error("token失效");
        //清除token
       // localStorage.removeItem("eleToken");
       // router.push('/login');
    //}
    return Promise.reject(err);
})
export default axios;